package com.wenheng.controller;

import com.alibaba.fastjson.JSONObject;
import com.wenheng.client.SeckillClient;

import com.wenheng.service.SeckillService;
import com.wenheng.utils.DtoUtils;
import com.wenheng.vo.entity.Seckill;
import com.wenheng.vo.entity.Seckillproduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Auther: 文恒
 * @Date: 2021/3/24 - 03 - 24 -9:52
 * @Description:com.wenheng.controller
 * @version:1.0
 */
@RestController
public class SeckillController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private SeckillClient seckillClient;
    //生成秒杀活动
    @PostMapping("/seckill/GenerateKill")
    public Map<String,Object> GenerateKill(@RequestBody Seckill seckill){
        if(seckillService.saveSeckillInfo(seckill) != -1 ){
            stringRedisTemplate.opsForValue().set("killInfo", JSONObject.toJSONString(seckill));
            return DtoUtils.success("生成成功");
        }else{
            return DtoUtils.file("生成失败");
        }
    }

    /**
     * 调用商品服务 查询出相关商品
     * @param productId 商品id
     * @return
     */
    @GetMapping("/seckill/findKill/{productId}")
    public Map<String,Object>findKill(@PathVariable("productId") Integer productId){
        //调用商品服务 查询出相关商品
        Map<String,Object> map=seckillClient.findSeckill(productId);
        //EasybuyProduct easybuyProduct = (EasybuyProduct)map.get("data");
        return map;
    }

    /**
     * 绑定商品  设置相关信息 -- 限制库存 初始化信息
     * 开始准备活动 秒杀业务按照时间开始
     * @param list
     * @return
     */
    @PostMapping("/seckill/bindingKill")
    public Map<String,Object> bindingKill(@RequestBody List<Seckillproduct> list){
        try {
            StringBuffer seckillId = new StringBuffer();
            int i =0;
            Integer num = 0;
            for (Seckillproduct seckillproduct : list) {
                if(i == 0) {
                    num = seckillproduct.getSeckillid();
                }
                i++;
                seckillService.saveBindingSeckillInfo(seckillproduct);
                if(list.size() <= i ){
                    seckillId.append(seckillproduct.getId());
                }else{
                    seckillId.append(seckillproduct.getId()+",");
                }
            }
            Seckill seckill = new Seckill();
            seckill.setId(num);
            seckill.setCommoditynum(seckillId.toString());
            //更新数据库记录
            seckillService.updateSeckillcommodityNum(seckill);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtils.file("活动添加失败！！！");
        }
        //查询秒杀活动
        //设置sentinel接口访问时间 * ？gateway * ？ RMQ 设置接口访问时间 *)  ？ redis设置接口访问时间 (/)
        stringRedisTemplate.opsForValue().set("kill", JSONObject.toJSONString(list));
        return DtoUtils.success("活动添加成功！！");
    }

    //管理员确认开启活动
    public String oepn(Integer activityId){
        //执行查询方法  并将内容存入到redis中   key之后中加入一个活动的id 以保证活动唯一性
//        stringRedisTemplate.opsForValue().set("kill", JSONObject.toJSONString(list)); //商品信息
//        stringRedisTemplate.opsForValue().set("killInfo", JSONObject.toJSONString(list));//活动信息
        return "";
    }

}
